<template>
  <div class="alipay-config gray-box top-redius border-bottom-gray">
    <div class="title">
      {{ $t('sysManagement.alipayPaymentConfiguration') }}
    </div>
    <el-form
      ref="dataFormRef"
      label-width="180px"
      class="set-form"
      label-position="left"
      :rules="dataRule"
      :model="dataForm"
      @submit.prevent
      @keyup.enter="onSubmit(dataFormRef)"
    >
      <el-form-item
        :label="$t('sysManagement.alipayAppId')"
        style="width:640px"
        prop="appId"
      >
        <el-input
          v-model="dataForm.appId"
          :placeholder="$t('sysManagement.alipayAppId')"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="$t('sysManagement.appCertPublicKey')"
        style="width:640px"
        prop="appCertContent"
      >
        <el-input
          v-model="dataForm.appCertContent"
          type="textarea"
          rows="10"
          :placeholder="$t('sysManagement.appCertPublicKey')"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="$t('sysManagement.alipayCertPublicKey')"
        style="width:640px"
        :placeholder="$t('sysManagement.alipayCertPublicKey')"
        prop="alipayCertContent"
      >
        <el-input
          v-model="dataForm.alipayCertContent"
          type="textarea"
          rows="10"
          :label="$t('sysManagement.alipayRootCertPath')"
          :placeholder="$t('sysManagement.alipayCertPublicKey')"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="$t('sysManagement.alipayRootCertPath')"
        :placeholder="$t('sysManagement.alipayRootCertPath')"
        style="width:640px"
        prop="alipayRootCertContent"
      >
        <el-input
          v-model="dataForm.alipayRootCertContent"
          type="textarea"
          rows="10"
          :placeholder="$t('sysManagement.alipayRootCertPath')"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="$t('sysManagement.appPrivateKey')"
        style="width:640px"
        prop="appPrivateKey"
      >
        <el-input
          v-model="dataForm.appPrivateKey"
          type="textarea"
          rows="10"
          :placeholder="$t('sysManagement.appPrivateKey')"
          controls-position="right"
        />
      </el-form-item>

      <div
        class="default-btn"
        @click="onSubmit(dataFormRef)"
      >
        {{ $t('sysManagement.save') }}
      </div>
    </el-form>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'

const emit = defineEmits(['refreshDataList'])
const dataFormRef = ref(null)
const dataForm = reactive({
  appId: null,
  appCertPath: null,
  appCertContent: null,
  alipayCertPath: null,
  alipayCertContent: null,
  alipayRootCertPath: null,
  alipayRootCertContent: null,
  appPrivateKey: null
})

const dataRule = {
  appId: [
    { required: true, message: $t('sysManagement.appNullTips'), trigger: 'blur' }
  ],
  appCertContent: [
    { required: true, message: $t('sysManagement.appCertPublicKeyNullTips'), trigger: 'blur' }
  ],
  alipayCertContent: [
    { required: true, message: $t('sysManagement.alipayCertPublicKeyNullTips'), trigger: 'blur' }
  ],
  alipayRootCertContent: [
    { required: true, message: $t('sysManagement.alipayRootCertPathNullTips'), trigger: 'blur' }
  ],
  appPrivateKey: [
    { required: true, message: $t('sysManagement.appPrivateKeyNullTips'), trigger: 'blur' }
  ]
}

onMounted(() => {
  init()
})

const init = () => {
  nextTick(() => {
    http({
      url: http.adornUrl('/sys/pconfig/info/' + 'ALIPAY_CONFIG'),
      method: 'get',
      params: http.adornParams()
    }).then(({ data }) => {
      if (data) {
        Object.assign(dataForm, JSON.parse(data))
      }
    })
    dataFormRef.value?.resetFields()
  })
}
// 表单提交
const onSubmit = async formEl => {
  if (!formEl) return
  await formEl.validate(valid => {
    if (!valid) return
    http({
      url: http.adornUrl('/sys/pconfig/save'),
      method: 'post',
      data: http.adornData({
        paramKey: 'ALIPAY_CONFIG',
        paramValue: JSON.stringify(dataForm),
        remark: $t('sysManagement.alipayPaymentConfiguration')
      })
    }).then(() => {
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          emit('refreshDataList')
        }
      })
    })
  })
}

</script>

<style lang="scss" scoped>
@use '../index.scss';
</style>
